(0, require("../../../@babel/runtime/helpers/interopRequireDefault").default)(require("../../../utils/util.js"));

var t = getApp().globalData.api, a = getApp();

Page({
    data: {
        data: null,
        couponId: -1,
        remarks: "",
        orderCnt: [],
        showScroll: "pkgItemList",
        showModalStatus: !1, 
        haveOrder: !1,
        card: "",
        dataConsult: [],
        agreenmentInfoList: [],
        agreenmentInfoSelect: {
            isShow: !1,
            content: null,
            title: null
        },       
        yymobie:'',
        unametjfun:'',
        ucardtjfun:'',
        markfun:'',
        goodsid:'',
        seldate:'',
        totalprice:0,
        
    },
    onLoad: function(e) {
        // selectedTimeStr
        var o = this, s = this;
        console.log(e);
        e.hasOwnProperty("seldate") && null != e.seldate && "" != e.seldate && this.setData({
            seldate: e.seldate
        }), e.hasOwnProperty("card") && null != e.card && "" != e.card && this.setData({
            card: e.card
        });
        var n = e.goodsid, r = e.setNumber;
        var openid = wx.getStorageSync('openid');
        t.post1("wechat/Member/getUserInfo", {
            openid: openid
        }).then(function(t) {  
            return o.setData({
                seldate:e.seldate,
                goodsid:e.goodsid,
                userPhone: t.data.mobileu
            }), !0;
        }, function(t) {
            wx.navigateTo({
                url: "/pages/login/login"
            });
        }).then(function(o) {
            if (o) {
                var i = n.toString().split(","), d = r.toString().split(","), u = [];
                if (console.log(e), i.length === d.length) {
                    for (var c = 0; c < i.length; c++) {
                        var l = {
                            goodsid: i[c],
                            orderCnt: d[c]
                        };
                        e.addItem && (l.addItemKey = decodeURIComponent(e.addItem)), u.push(l);
                    }
                    var coupinfo= {
                        status: "6D02",
                        appSource: a.globalData.appInfo.zkSource
                    };
                    var para = {
                        cardNo: s.data.card,
                        orderInfos: JSON.stringify(u),
                        couponSearchVO: JSON.stringify(coupinfo)
                    };
                    s.setData({
                        options: para
                    });

                    t.post1("wechat/home/getPreOrderInfo",para).then(function(t) {
                        var e = t.data;
                        e.orderPkgInfos.forEach(function(t) {
                            // t.checkers.forEach(function(t) {
                            //     s.data.card || (t.bespeakStatus = 9503), t.isHasData = !1, "" != s.data.selectedTimeStr && (t.bespeakDate = s.data.selectedTimeStr);
                            // });
                        });
                        // a.globalData.coupon = e.couponList;
                        // for (var o = [], n = [], r = e.couponList || [], i = 0; i < r.length; i++) {
                        //     var d = r[i];
                        //     d.ifDisable ? o.push(d) : n.push(d);
                        // }
                        // e.overdueCouponList = o, e.validCouponList = n;
                         s.setData({
                            data: e,
                            customers: e.amount,
                            totalprice: e.amount,
                        });
                        // 重新计算
                        // s.maximum();
                    });
                } else wx.switchTab({
                    url: "/pages/homepages/homepages"
                });
            }
        });
    },
    
       
    saveData: function(t) {
        wx.setStorageSync("options", t);
    },
    getAgreemeetData: function() {
        var a = this;
        t.postPort("findStaticPageConfigByCode", {
            code: [ "XDXZ" ]
        }).then(function(t) {
            if ("SUCCESS" == t.result) {
                var e = a.data.agreenmentInfoList;
                t.values.forEach(function(t, a) {
                    1 != a && 2 != a && 3 != a || e.unshift({
                        ishow: !1,
                        content: t.content,
                        title: t.title
                    });
                }), a.setData({
                    dataConsult: t.values[0].childList,
                    agreenmentInfoList: e
                });
            }
        }, function(t) {});
    },
    onReady: function() {
        // this.getAgreemeetData();
    },
    onShow: function() {
        if (this.data.coupon >= 0) {
            var t, a = this.data.data;
            this.data.coupon == this.data.couponId ? (t = -1, a.amount = this.data.customers) : (t = this.data.coupon, 
            a.immeDisPrice >= this.data.customers ? (t = -1, wx.showToast({
                title: "已是最大优惠,无需使用优惠卷",
                icon: "none"
            })) : "6H01" == a.validCouponList[t].couponRuleType ? a.amount = this.data.customers - a.validCouponList[t].disPrice : 100 == a.validCouponList[t].disRatio ? a.amount = 0 : a.amount = this.data.customers * (a.validCouponList[t].disRatio / 100)), 
            this.setData({
                couponId: t,
                data: a
            });
        }
    },
    maximum: function() {
        if (this.data.data.validCouponList.length > 0) {
            for (var t = [], a = {}, e = -1, o = 0; o < this.data.data.validCouponList.length; o++) "6H01" == this.data.data.validCouponList[o].couponRuleType && t.push(this.data.data.validCouponList[o]);
            t.sort(function(t, a) {
                return a.disPrice - t.disPrice;
            });
            for (var s = 0; s < t.length; s++) if (t[s].disPrice < this.data.customers && !a.id) {
                a = t[s];
                break;
            }
            if (a.id) {
                for (var n = 0; n < this.data.data.validCouponList.length; n++) if (this.data.data.validCouponList[n].id == a.id) {
                    e = n;
                    break;
                }
                this.setData({
                    couponId: e
                }), -1 != this.couponId && this.setData({
                    "data.amount": this.data.data.amount - a.disPrice
                });
            }
        }
    },
    messageToggle: function(t) {
        var a = t.currentTarget.dataset, e = a.key1, o = a.key2, s = this.data.data;
        "9503" == s.orderPkgInfos[e].checkers[o].bespeakStatus ? s.orderPkgInfos[e].checkers[o].bespeakStatus = "9501" : s.orderPkgInfos[e].checkers[o].bespeakStatus = "9503", 
        this.setData({
            data: s
        });
    },
    addChecker: function(t) {
        var a = t.currentTarget.dataset, e = a.key1, o = a.key2, s = this.data.data, n = "/pages/order/checker-list/checker-list";
        n += "?key1=" + e, n += "&key2=" + o, n += "&suitSex=" + s.orderPkgInfos[e].suitSex, 
        n += "&suitMarried=" + s.orderPkgInfos[e].suitMarried, wx.navigateTo({
            url: n
        });
    },
    formModel: function(t) {
        var a = t.currentTarget.dataset, e = a.key1, o = a.key2, s = a.type, n = this.data.data;
        n.orderPkgInfos[e].checkers[o][s] = t.detail.value, this.setData({
            data: n
        });
    },
    umessage: function(t) {
        "userPhone" == t.currentTarget.dataset.type ? this.setData({
            userPhone: t.detail.value
        }) : this.setData({
            remarks: t.detail.value
        });
    },

    yymobiefun: function(t) {
        // console.log(t);        
        this.setData({
          yymobie: t.detail.value
        }) 
    },
    unametjfun: function(t) {
        this.setData({
            unametj: t.detail.value
         }) 
     },

     ucardtjfun: function(t) {
        this.setData({
            ucardtj: t.detail.value
         }) 
     },


     markfun: function(t) {
        this.setData({
            mark: t.detail.value
         }) 
     },

    

    unameget: function(t) {
        "userPhone" == t.currentTarget.dataset.type ? this.setData({
            userPhone: t.detail.value
        }) : this.setData({
            remarks: t.detail.value
        });
    },

    dataTimeShow: function(t) {
        var a = t.currentTarget.dataset, e = (a.v1, a.key1), o = a.key2;
        this.data.timek1 = e, this.data.timek2 = o, this.alertComponent("dataList");
    },
    showPkInfo: function() {
        
    },
    showNotice: function() {
        this.alertComponent("notice");
    },
    showPrivilege: function() {
        this.alertComponent("privilege");
    },
    showAgrentmentTap: function(t) {
        var a = t.currentTarget.dataset.index, e = {
            isShow: !1,
            content: null,
            title: null
        };
        this.data.agreenmentInfoList.forEach(function(t, o) {
            o == a && (e.isShow = !0, e.content = t.content, e.title = t.title);
        }), this.setData({
            agreenmentInfoSelect: e
        }), this.selectComponent("#orderagreenment").powerDrawer();
    },
    alertComponent: function(t) {
        this.selectComponent("#" + t).powerDrawer();
    },
    powerDrawer: function() {
        var t = this.data.showModalStatus, a = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });
        this.animation = a, a.translateY(200).step(), this.setData({
            animationData: a.export()
        }), setTimeout(function() {
            a.translateY(0).step(), this.setData({
                animationData: a
            }), t && this.setData({
                showModalStatus: !1
            });
        }.bind(this), 100), t || this.setData({
            showModalStatus: !0
        });
    },
    atTime: function(t) {
        var a = this.data.data;
        a.orderPkgInfos[this.data.timek1].checkers[this.data.timek2].bespeakDate = t.detail, 
        this.setData({
            data: a
        });
    },
    discount: function() {
        this.saveData(this.data.options), this.setData({
            coupon: -1
        });
        var t = "/pages/order/discount-coupon/discount-coupon";
        "-1" != this.data.couponId && (t += "?u=" + this.data.couponId), wx.navigateTo({
            url: t
        });
    },
    submitOrder: function() {
        this.data.haveOrder='';
        if (this.data.haveOrder) wx.showToast({
            title: "重复订单",
            icon: "none"
        }); else {
            for (var e = this, o = [], s = this.data.data.orderPkgInfos, n = getApp().globalData.promotionSourceCode, r = 0; r < s.length; r++) {
                var i = {
                    gdsId: s[r].id,
                    gdsNum: s[r].orderCnt,
                    checkers: []
                };
                s[r].addItems && (i.addItems = s[r].addItems.map(function(t) {
                    return t.id;
                }));
                // for (var d = 0; d < s[r].checkers.length; d++) {
                //     var u = s[r].checkers[d];
                //     "9503" == u.bespeakStatus && ("0101" != s[r].suitSex && "0102" != s[r].suitSex || (u.sex = s[r].suitSex), 
                //     "0201" != s[r].suitMarried && "0202" != s[r].suitMarried || (u.marriage = s[r].suitMarried)), 
                //     i.checkers.push(u);
                // }
                o.push(i);
            }
            var openid = wx.getStorageSync('openid');
            var c = {
                yymobie: this.data.yymobie,
                unametj: this.data.unametj,
                ucardtj: this.data.ucardtj,
                mark: this.data.mark,
                goodsid: this.data.goodsid,
                hosid:this.data.hosId,
                seldate: this.data.seldate,
                openid:openid,
                totalprice: this.data.totalprice*1/100
                // orderType: "9J01",
                // orderSource: a.globalData.appInfo.source,
                // orderKind: this.data.data.orderKind,              
                // sysCode: "01",
                // platSource: a.globalData.appInfo.zkPlatSource,
                // gdsCheckers: o
            };
            // "-1" != this.data.couponId && (c.couponId = this.data.data.validCouponList[this.data.couponId].id, 
            // c.memberCouponId = this.data.data.validCouponList[this.data.couponId].memberCouponId), 
            // this.data.card && (c.cardNo = this.data.card);
            // var l = getApp().globalData.gdt_vid;
            // null != l && (c.clickId = l), n && (c.promotionSourceCode = n);
            // var h = wx.getStorageSync("disCode");
            // c.disCode = h || getApp().globalData.keyTjgj.disCode, console.log(c);
      
           
            t.post1("wechat/home/submitOrder",c, !0).then(function(t) {
                var a;
                e.setData({
                    haveOrder: !0
                }), e.data.data.offlineAmt || e.data.card ? (a = "/pages/order/pay_succeed/pay_succeed?orderId=" + t.data, 
                wx.redirectTo({
                    url: a
                })) : e.readPay(t.data);
            }, function(t) {
                console.log(t), t.data.hasOwnProperty("result") && "ERROR" == t.data.result && wx.showToast({
                    title: t.data.message[0].message,
                    icon: "none",
                    duration: 2e3,
                    mask: !0
                });
            });
        }
    },
    readPay: function(e) {
        var o = '{"openid":"'.concat(wx.getStorageSync("openid"), '"}'), para = {
            ordersn: e,
            openid:wx.getStorageSync("openid"),
            paytype:0
        };
        t.post1("wechat/mall/order/getwxpay",para, !0).then(function(t1) {     
            wx.requestPayment({
                timeStamp: t1.data.timeStamp,
                nonceStr: t1.data.nonceStr,
                package: t1.data.package,
                signType: t1.data.signType,
                paySign: t1.data.paySign,
                success: function(t2) {
                    if("requestPayment:ok" == t2.errMsg){
                        // $ordersn = $this->request->param('ordersn');
                        // $openid = $this->request->param('openid');
                        // $paytype= $this->request->param('paytype');//WX：微信；  YY：余额
                        t.post1("wechat/home/updateOrder",para).then(function(t3) {   
                            if(1 == t3.code){
                                wx.redirectTo({
                                    url: "/pages/order/pay_succeed/pay_succeed?orderId=" + e
                                });
                            }else{
                                wx.showToast({
                                    title: "更新订单状态失败",
                                    icon: "none",
                                    duration: 2e3,
                                    mask: !0
                                });
                            }

                        })


                      

                    }
                     
                },
                fail: function(t) {
                    wx.redirectTo({
                        url: "/pages/usercenter/userOrder/order_particulars/order-particulars?orderId=" + e
                    });
                },
                complete: function(t) {}
            });
        }, function(t) {
            0 == t.code && wx.showToast({
                title: t.data.message[0].message,
                icon: "none",
                duration: 2e3,
                mask: !0
            });
        });
    },
    onUnload: function() {
        wx.removeStorageSync("options");
    }
});